/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
UART_InitTypeDef UART_InitStructure;

/* Private function prototypes -----------------------------------------------*/
UartRxBufDef Uart1Rx, Uart2Rx;
UART_TypeDef* UartInstance[2] = {UART1, UART2};
uint8_t UartIndex;

/* Private functions ---------------------------------------------------------*/
static void RCC_Configuration(void);
static void GPIO_Configuration(void);
static void NVIC_Configuration(void);
static void Uart1SendDatas(uint8_t *p, uint8_t len);
static void Uart2SendDatas(uint8_t *p, uint8_t len);

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */

  /* Configure clock GPIO, UARTs */
  RCC_Configuration();

  /* Configure GPIO Pin Tx/Rx for Uart communication */
  GPIO_Configuration();

  /* Configure NVIC */
  NVIC_Configuration();

  /* UART1 configuration --------------------------------------------------*/
  /*configured as follow:
  - BaudRate = 9600 baud
  - Word Length = 8 Bits
  - One Stop Bit
  - No parity
  - Receive and transmit enabled
  */
  UART_InitStructure.UART_BaudRate = 9600;
  UART_InitStructure.UART_WordLength = UART_WordLength_8b;
  UART_InitStructure.UART_StopBits = UART_StopBits_1;
  UART_InitStructure.UART_Parity = UART_Parity_No;
  UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;

  UART_Init(UART1, &UART_InitStructure);

  /* Enable UART1 Receive and IDLE interrupts */
  UART_ITConfig(UART1, UART_IT_RXNE, ENABLE);
  UART_ITConfig(UART1, UART_IT_IDLE, DISABLE);

  Uart1Rx.Index = 0;
  Uart1Rx.Len = 0;
  Uart1Rx.FinishedFlag = RESET;

  /* Enable the UART1 */
  UART_Cmd(UART1, ENABLE);

  /* UART2 configuration --------------------------------------------------*/
  /*configured as follow:
  - BaudRate = 9600 baud
  - Word Length = 8 Bits
  - One Stop Bit
  - No parity
  - Receive and transmit enabled
  */
  UART_InitStructure.UART_BaudRate = 9600;
  UART_InitStructure.UART_WordLength = UART_WordLength_8b;
  UART_InitStructure.UART_StopBits = UART_StopBits_1;
  UART_InitStructure.UART_Parity = UART_Parity_No;
  UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
  UART_Init(UART2, &UART_InitStructure);

  /* Enable UART2 Receive and IDLE interrupts */
  UART_ITConfig(UART2, UART_IT_RXNE, ENABLE);
  UART_ITConfig(UART2, UART_IT_IDLE, DISABLE);

  Uart2Rx.Index = 0;
  Uart2Rx.Len = 0;
  Uart2Rx.FinishedFlag = RESET;

  /* Enable the UART2 */
  UART_Cmd(UART2, ENABLE);

  /* Infinite Loop */
  while(1)
  {
    if( Uart1Rx.FinishedFlag == SET )
    {
      Uart1Rx.FinishedFlag = RESET;
      Uart1SendDatas(Uart1Rx.Data, Uart1Rx.Len);
    }

    if( Uart2Rx.FinishedFlag == SET )
    {
      Uart2Rx.FinishedFlag = RESET;
      Uart2SendDatas(Uart2Rx.Data, Uart2Rx.Len);
    }
  }
}

/**
  * @brief  UART1 Send some data.
  * @param  p - the start address of data to be send
  * @param  len - the len of data to be send
  * @retval None
  */
static void Uart1SendDatas(uint8_t *p, uint8_t len)
{
  while( len -- )
  {
    UART_SendData(UART1, *p++);

    while( UART_GetFlagStatus(UART1, UART_FLAG_TC) == RESET )
    {
    }
  }
}

/**
  * @brief  UART2 Send some data.
  * @param  p - the start address of data to be send
  * @param  len - the len of data to be send
  * @retval None
  */
static void Uart2SendDatas(uint8_t *p, uint8_t len)
{
  while( len -- )
  {
    UART_SendData(UART2, *p++);

    while( UART_GetFlagStatus(UART2, UART_FLAG_TC) == RESET )
    {
    };
  }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
static void RCC_Configuration(void)
{
  /* Enable GPIO clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);

  /* Enable UART1 clock */
  RCC_APBPeriph2ClockCmd(RCC_APBPeriph2_UART1, ENABLE);

  /* Enable UART2 clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_UART2, ENABLE);
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
static void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /*
    UART1_TX:PA3  AF1
  	UART1_RX:PA2  AF1

  	UART2_TX:PC3   AF6
  	UART2_RX:PC4   AF6
  */

  /* UART1 Pins configuration ************************************************/

  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);

  /* Configure pins as AF pushpull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  /* RX Pull-Up can filter noise*/
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* UART2 Pins configuration ************************************************/

  /* Connect pin to Periph */
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_6);

  /* Configure pins as AF pushpull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;   /* RX Pull-Up can filter noise*/
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
static void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* UART1 IRQ Channel configuration */
  NVIC_InitStructure.NVIC_IRQChannel = UART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* UART2 IRQ Channel configuration */
  NVIC_InitStructure.NVIC_IRQChannel = UART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}



#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


